// pages/famous_details/famous_details.js
const app = getApp()
const http = require("../../config/api.js");
const user = require("../../utils/login.js");
Page({
  data: {
    bool: true,
    query: {
      imgUrl: "",
      artistCode: "",
      displayType: 1,
      px: 2,
      cur: 1,
      pageSize: 10
    },
    artInfo: {},
    worksList: []
  },
  onLoad: function (options) {
    let artistCode = options.artistCode;
    this.setData({
      ['query.artistCode']: artistCode,
      api: app.globalData.baseUrlApiTest,
      imgUrl: app.globalData.baseUrlImg,
    })
  },
  onShow: function () {
    this.fnGetArtistInfo();
    this.fnGetArtistAlbumList();
    let time = Math.round(new Date().getTime());
    let log = {
      //日志类型：unite.通用业务；visit.访问数据；click.点击数据；order.订购数据；error.错误日志
      logType: "visit",
      msg: JSON.stringify({
        mac: app.globalData.userId,
        page: "xcxyrinfo",
        pageName: "梨园行名家详情",
        time: time,
        appVersionName: "小程序",
        item: 65,
        nodeCode: "001000",
        project: "lyhad",
        userId: app.globalData.memberId,
        deviceId: app.globalData.userId,
        uuid: user.getUuid(),
        pageLevel: "1"
      })
    }
    this.uniteLog(log)
  },
  fnGetArtistInfo() {
    let data = {
      artistCode: this.data.query.artistCode,
      displayType: this.data.query.displayType
    }
    http.lhxRequest("api_getArtistInfo", data).then(res => {
      if (res.code === 10000000) {
        this.setData({
          artInfo: res.artist
        })
      }
    })
  },
  fnGetArtistAlbumList() {
    let data = {
      artistCode: this.data.query.artistCode,
      px: this.data.query.px,
      cur: this.data.query.cur,
      pageSize: this.data.query.pageSize
    }
    let bool = this.data.bool;
    if (!bool) {
      return false;
    };
    http.lhxRequest("api_getArtistAlbumList", data).then(res => {
      let worksList = this.data.worksList.length > 0 ? this.data.worksList : [];
      for (let i in res.pb.dataList) {
        worksList.push(res.pb.dataList[i])
      }
      if (res.pb.dataList.length <= res.pb.pageSize && res.pb.cur === res.pb.totalPage) {
        this.setData({
          bool: false
        })
      }
      if (res.code === 10000000) {
        this.setData({
          worksList: worksList
        })
      }
    })
  },
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    let that = this;
    that.setData({
      worksList: [],
      ['query.cur']: 1,
      bool: true
    })
    that.fnGetArtistAlbumList()
  },
  onReachBottom: function () {
    let that = this;
    let bool = that.data.bool;
    that.setData({
      ['query.cur']: that.data.query.cur += 1,
    })
    if (!bool) {
      wx.showToast({
        title: "没有更多数据",
        icon: "none",
        duration: 2000,
      });
    }
    that.fnGetArtistAlbumList();
  },
  // 跳到详情
  fnJumpDetail(e) {
    const code = e.currentTarget.dataset.code;
    wx.navigateTo({
      url: "/pages/album_details/album_details?code=" + code,
    })
  },
  // 日志
  uniteLog(data) {
    http.lhxRequest("api_log", data).then(res => {
      if (res.code === 10000000) {
        console.log("日志")
      }
    })
  }
})